/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.SmsTemplatePo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  SmsTemplatePoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class SmsTemplateSqlProvider {
	public static final String TABLE_NAME = "sms_template";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param smsTemplate
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSmsTemplates(final SmsTemplatePo smsTemplate) {
		return new CustomerSQL() {{
			SELECT("category_id,category_name,code,content,create_time,creator_id,description,id,name,platform_id,platform_name,update_time,updater_id ");
			FROM(TABLE_NAME);
			
			if (null != smsTemplate.getCategoryId()) {
				WHERE("category_id=#{categoryId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getCategoryName())) {
				WHERE("category_name=#{categoryName}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getCode())) {
				WHERE("code=#{code}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getContent())) {
				WHERE("content=#{content}");
			}
			if (null != smsTemplate.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getDescription())) {
				WHERE("description=#{description}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getName())) {
				WHERE("name=#{name}");
			}
			if (null != smsTemplate.getPlatformId()) {
				WHERE("platform_id=#{platformId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getPlatformName())) {
				WHERE("platform_name=#{platformName}");
			}
			if (null != smsTemplate.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
			if(StringUtils.isNotBlank(smsTemplate.getSortField())) {
				ORDER_BY(smsTemplate.getSortField() + (StringUtils.isNotBlank(smsTemplate.getSortMethod()) ? "desc" : smsTemplate.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != smsTemplate.getStart() && null != smsTemplate.getSize()) {
				LIMIT(smsTemplate.getStart(), smsTemplate.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param smsTemplate
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSmsTemplateCount(final SmsTemplatePo smsTemplate) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (null != smsTemplate.getCategoryId()) {
				WHERE("category_id=#{categoryId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getCategoryName())) {
				WHERE("category_name=#{categoryName}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getCode())) {
				WHERE("code=#{code}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getContent())) {
				WHERE("content=#{content}");
			}
			if (null != smsTemplate.getCreatorId()) {
				WHERE("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getDescription())) {
				WHERE("description=#{description}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getName())) {
				WHERE("name=#{name}");
			}
			if (null != smsTemplate.getPlatformId()) {
				WHERE("platform_id=#{platformId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getPlatformName())) {
				WHERE("platform_name=#{platformName}");
			}
			if (null != smsTemplate.getUpdaterId()) {
				WHERE("updater_id=#{updaterId}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findSmsTemplateById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param smsTemplate
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addSmsTemplate(final SmsTemplatePo smsTemplate) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (null != smsTemplate.getCategoryId()) {
				VALUES("category_id","#{categoryId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getCategoryName())) {
				VALUES("category_name","#{categoryName}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getCode())) {
				VALUES("code","#{code}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getContent())) {
				VALUES("content","#{content}");
			}
			if (null != smsTemplate.getCreatorId()) {
				VALUES("creator_id","#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getDescription())) {
				VALUES("description","#{description}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getName())) {
				VALUES("name","#{name}");
			}
			if (null != smsTemplate.getPlatformId()) {
				VALUES("platform_id","#{platformId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getPlatformName())) {
				VALUES("platform_name","#{platformName}");
			}
			if (null != smsTemplate.getUpdaterId()) {
				VALUES("updater_id","#{updaterId}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param smsTemplate
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateSmsTemplate(final SmsTemplatePo smsTemplate) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (null != smsTemplate.getCategoryId()) {
				SET("category_id=#{categoryId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getCategoryName())) {
				SET("category_name=#{categoryName}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getCode())) {
				SET("code=#{code}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getContent())) {
				SET("content=#{content}");
			}
			if (null != smsTemplate.getCreatorId()) {
				SET("creator_id=#{creatorId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getDescription())) {
				SET("description=#{description}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getName())) {
				SET("name=#{name}");
			}
			if (null != smsTemplate.getPlatformId()) {
				SET("platform_id=#{platformId}");
			}
			if (StringUtils.isNotBlank(smsTemplate.getPlatformName())) {
				SET("platform_name=#{platformName}");
			}
			if (null != smsTemplate.getUpdaterId()) {
				SET("updater_id=#{updaterId}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delSmsTemplate(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
